package cn.jdx.filter;


import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@Order(1) //(4).设置优先级(值越小，越先执行)
@WebFilter(filterName = "MyFilter",urlPatterns = "/filter") //(3).添加过滤器注解
public class MyFilter implements Filter { //(1).实现 javax.servlet.Filter 接口方法

    @Override
    public void init(FilterConfig filterConfig) {
        System.out.println("[过滤器]:init()");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("[过滤器]:doFilter()");
        //filterChain 责任链的设计模式，编写过滤器的时候，必须要调用该方法，否则其他的过滤器将无法相继执行
        filterChain.doFilter(servletRequest,servletResponse); //(2).完成责任链的调用
    }

    @Override
    public void destroy() {
        System.out.println("[过滤器]:destroy()");
    }
}
